package ifi.sma.group5.agents;

import ifi.sma.group5.db.EtudiantModel;
import ifi.sma.group5.gui.EtudiantGui;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;

public class Etudiant extends Agent {
	
	/**
	 * 
	 */
	private EtudiantGui gui;
	private String message;
	private EtudiantModel etudiantModel;
	
	protected void setup() {
		//System.out.println("Hello world");
		super.setup();
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());
		
		ServiceDescription sd = new ServiceDescription();
		sd.setType("etudiant-demander1");
		sd.setName("JADE-book-trading");
		dfd.addServices(sd);
		
		
		gui = new EtudiantGui();
		
		
		addBehaviour(new AssistantService());
		
		try{
			DFService.register(this, dfd);
		}catch(FIPAException fe) {
			fe.printStackTrace();
		}
	}
	
	private class AssistantService extends CyclicBehaviour {
		@Override
		public void action() {
			// TODO Auto-generated method stub
			
			//chercher d'un partenaire
			DFAgentDescription template= new DFAgentDescription();
			ServiceDescription sd = new ServiceDescription();
			sd.setType("etudiant_demander");
			template.addServices(sd);
			
			try {
				
				DFAgentDescription[] result = DFService.search(myAgent, template);
				if(result.length >=0){
					AID receiverName = (AID)result[0].getName();
					ACLMessage cfp = new ACLMessage(ACLMessage.CFP);
					cfp.setContent("I sell seashells at $10/kg" );
					
					cfp.addReceiver(receiverName);
					
					cfp.setConversationId("demander-test");
					cfp.setReplyWith("cfp"+ System.currentTimeMillis()); 
					myAgent.send(cfp);			
					//System.out.println("Envoyer des message");
				}
			} catch (FIPAException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
	}

}
